package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companyreceiver.CompanyReceiverAddRequest;
import com.nanyang.tms.api.dto.request.companyreceiver.CompanyReceiverDelRequest;
import com.nanyang.tms.api.dto.request.companyreceiver.CompanyReceiverQueryRequest;
import com.nanyang.tms.api.dto.request.companyreceiver.CompanyReceiverUpdateRequest;
import com.nanyang.tms.api.dto.response.companyreceiver.CompanyReceiverPageResponse;
import com.nanyang.tms.api.dto.response.companyreceiver.CompanyReceiverResponse;
import com.nanyang.tms.api.service.companyreceiver.CompanyReceiverRpcService;
import com.nanyang.tms.dao.company.CompanyReceiverDao;
import com.nanyang.tms.domain.company.CompanyReceiverDO;
import com.nanyang.tms.domain.company.CompanyReceiverQuery;
import com.nanyang.tms.domain.company.CompanyReceiverVO;
import com.nanyang.tms.manager.company.CompanyReceiverManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyReceiverRpcServiceImpl implements CompanyReceiverRpcService {

    @Autowired
    private CompanyReceiverManager companyReceiverManager;
    @Autowired
    private CompanyReceiverDao companyReceiverDao;
    @Override
    public ApiResult<PageBean<CompanyReceiverPageResponse>> listCompanyReceiver(CompanyReceiverQueryRequest param) {
        ApiResult<PageBean<CompanyReceiverPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyReceiverQuery query = new CompanyReceiverQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyReceiverVO> companyReceiverList = companyReceiverManager.listCompanyReceiver(query);
            PageBean<CompanyReceiverPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyReceiverList.getTotalItem());
            BeanUtils.copyListProperties(companyReceiverList.getData(), resultList.getData(), CompanyReceiverPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<CompanyReceiverResponse> selectByCode(CompanyReceiverQueryRequest param) {
        ApiResult<CompanyReceiverResponse> result = new ApiResult<>();
        try {
            CompanyReceiverResponse  companyReceiverResult = new CompanyReceiverResponse();
            CompanyReceiverQuery query=new CompanyReceiverQuery();
            BeanUtils.copyProperties(param, query);
            CompanyReceiverVO companyReceiverVo = companyReceiverDao.selectByCode(query);
            if(Objects.isNull(companyReceiverVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(companyReceiverVo,  companyReceiverResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( companyReceiverResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveCompanyReceiver(CompanyReceiverAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyReceiverDO companyReceiverDo = new  CompanyReceiverDO();
            BeanUtils.copyProperties(param,companyReceiverDo);
            companyReceiverDo.setGmtCreatePin(param.getPin());
            companyReceiverManager.saveCompanyReceiver(companyReceiverDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateCompanyReceiver(CompanyReceiverUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyReceiverDO companyReceiverDo = new  CompanyReceiverDO();
            BeanUtils.copyProperties(param,companyReceiverDo);
            companyReceiverDo.setGmtModifiedPin(param.getPin());
            companyReceiverDao.update(companyReceiverDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> delCompanyReceiver(CompanyReceiverDelRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            CompanyReceiverDO companyReceiverDO=new CompanyReceiverDO();
            BeanUtils.copyProperties(param,companyReceiverDO);
            companyReceiverDO.setGmtModifiedPin(param.getPin());
            companyReceiverDao.delReceiver(companyReceiverDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
